# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=libvirt
pkgver=6.6.0
_ver="${pkgver/_rc/-rc}"
pkgrel=3
pkgdesc="A virtualization API for several hypervisor and container systems"
url="https://libvirt.org/"
arch="all"
options="!check"
license="LGPL-2.1-or-later"
_daemon_deps="bridge-utils dmidecode dnsmasq ip6tables iptables"
_client_deps="pm-utils gnutls-utils netcat-openbsd"
depends="lvm2"
depends_dev="libtirpc-dev"
makedepends="augeas-dev cyrus-sasl-dev device-mapper
	e2fsprogs-dev gnutls-dev libcap-ng-dev libgpg-error-dev
	netcf-dev libnl3-dev libxml2-dev libxslt-dev libtasn1-dev
	lvm2-dev lxc-dev libgcrypt-dev parted-dev perl pkgconfig
	eudev-dev zlib-dev yajl-dev libpcap-dev curl-dev libpciaccess-dev
	polkit-dev readline-dev linux-headers jansson-dev
	$depends_dev perl-xml-xpath
	fuse-dev util-linux libxml2-utils bash-completion $_daemon_deps $_client_deps
	automake autoconf libtool rpcsvc-proto py3-docutils
	"
install="$pkgname.post-install"
subpackages="$pkgname-dbg $pkgname-static $pkgname-libs $pkgname-dev $pkgname-doc $pkgname-client $pkgname-daemon
	$pkgname-lang $pkgname-lxc $pkgname-qemu $pkgname-uml::noarch $pkgname-vbox
	$pkgname-bash-completion:bashcomp:noarch"
source="https://libvirt.org/sources/libvirt-$pkgver.tar.xz
	libvirt.confd
	libvirt.initd
	libvirt-guests.confd
	libvirt-guests.initd
	virtlogd.initd
	virtlockd.initd
	musl-fix-includes.patch
	stderr-fix.patch
	libvirt-fork-exec-deadlock.patch
	libvirt-6.0.0-fix_paths_in_libvirt-guests_sh.patch
	"
if [ "$CARCH" = "x86_64" ]; then
	subpackages="$subpackages $pkgname-xen"
	makedepends="$makedepends xen-dev zfs"
fi
subpackages="$subpackages $pkgname-common-drivers:_common_drivers"

# secfixes:
#   6.6.0-r0:
#     - CVE-2020-14339
#   5.5.0-r0:
#     - CVE-2019-10168
#     - CVE-2019-10167
#     - CVE-2019-10166
#     - CVE-2019-10161

prepare() {
	default_prepare
	mkdir -p build
	cd build
	autoreconf -fi ../
}

build() {
	cd build
	# qemu sets libexec dir to /usr/lib/qemu
	export ac_cv_path_QEMU_BRIDGE_HELPER="/usr/lib/qemu/qemu-bridge-helper"
	[ "$CARCH" = x86_64 ] && local _xen_opt="--with-libxl"
	[ "$CARCH" = x86_64 ] && local _zfs_opt="--with-storage-zfs"
	../configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--libexec=/usr/lib/"$pkgname" \
		$_xen_opt \
		$_zfs_opt \
		--with-polkit \
		--with-storage-fs \
		--with-storage-lvm \
		--enable-static \
		--with-qemu \
		--with-qemu-group=kvm \
		--with-qemu-user=qemu
	make
}

package() {
	cd build
	make DESTDIR="$pkgdir" install
	install -Dm755 $srcdir/$pkgname.initd $pkgdir/etc/init.d/libvirtd
	install -Dm644 $srcdir/$pkgname.confd $pkgdir/etc/conf.d/libvirtd

	install -Dm755 $srcdir/$pkgname.initd $pkgdir/etc/init.d/libvirt-guests
	install -Dm644 $srcdir/$pkgname.confd $pkgdir/etc/conf.d/libvirt-guests

	install -Dm755 $srcdir/virtlogd.initd $pkgdir/etc/init.d/virtlogd
	install -Dm755 $srcdir/virtlockd.initd $pkgdir/etc/init.d/virtlockd

	install -d "$pkgdir"/lib/modules-load.d
	echo "tun" > "$pkgdir"/lib/modules-load.d/libvirt.conf

	rm -rf "$pkgdir"/usr/lib/charset.alias
	rmdir -p "$pkgdir"/usr/lib 2>/dev/null || true
}

dev() {
	default_dev
	mkdir -p "$subpkgdir"/usr/share/libvirt
	mv "$pkgdir"/usr/share/libvirt/api \
		"$subpkgdir"/usr/share/libvirt/
}

daemon() {
	cd "$builddir"
	pkgdesc="libvirt daemon package"
	depends="libvirt-client $_daemon_deps"
	replaces="libvirt"
	mkdir -p "$subpkgdir"/etc/$pkgname \
	 "$subpkgdir"/etc/logrotate.d \
	 "$subpkgdir"/usr/sbin
	mv "$pkgdir"/etc/init.d "$subpkgdir"/etc
	mv "$pkgdir"/etc/conf.d "$subpkgdir"/etc
	mv "$pkgdir"/lib/modules-load.d "$subpkgdir"/lib
	mv "$pkgdir"/etc/$pkgname/libvirtd.conf "$subpkgdir"/etc/libvirt/
	mv "$pkgdir"/usr/sbin/libvirtd "$subpkgdir"/usr/sbin/
	mv "$pkgdir"/etc/logrotate.d/libvirtd "$subpkgdir"/etc/logrotate.d/
}

client() {
	cd "$builddir"
	pkgdesc="libvirt client package"
	depends="libvirt $_client_deps"
	replaces="libvirt"
	mkdir -p "$subpkgdir"/etc/$pkgname "$subpkgdir"/usr/bin \
		"$subpkgdir"/usr/lib
	mv "$pkgdir"/etc/$pkgname/$pkgname.conf "$subpkgdir"/etc/libvirt/
	mv "$pkgdir"/usr/bin/* "$subpkgdir"/usr/bin/
}

static() {
	pkgdesc="$pkgname static libraries"
	mkdir -p "$subpkgdir"/usr/lib
	cd "$pkgdir"
	for _lib in  $(find . -name '*.a'); do
		mkdir -p "$subpkgdir"/${_lib%/*}
		mv "$_lib" "$subpkgdir"/${_lib%/*}/
	done
}

bashcomp() {
	depends=""
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"
	mkdir -p "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/share/bash-completion \
		"$subpkgdir"/usr/share/
}

_mv_driver() {
	local _name="$1" _drivers="$2" _pkg="$3"
	pkgdesc="$_name driver for libvirt"
	depends="$pkgname-daemon=$pkgver-r$pkgrel
		$pkgname-common-drivers=$pkgver-r$pkgrel"
	replaces="libvirt"
	if [ -n "$_pkg" ]; then
		install_if="$pkgname-daemon=$pkgver-r$pkgrel $_pkg"
	else
		install_if=
	fi
	local _dir=/usr/lib/libvirt/connection-driver
	mkdir -p "$subpkgdir"/$_dir \
		"$subpkgdir"/etc/libvirt \
		"$subpkgdir"/etc/logrotate.d
	local _d
	cd "$pkgdir"
	for _d in $_drivers; do
		find * -name "*$_d[.-]*" -o -name "*$_d" | while read _f; do
			mkdir -p "$subpkgdir"/${_f%/*}
			mv "$pkgdir"/$_f "$subpkgdir"/$_f
		done
	done
}

qemu() { _mv_driver "QEMU" qemu qemu; }
xen() { _mv_driver "XEN" "libxl xen" xen; }
lxc() { _mv_driver "LXC" lxc lxc; }
uml() { _mv_driver "UML" uml; }
vbox() { _mv_driver "VirtualBox" vbox; }

_common_drivers() {
	pkgdesc="Common libvirt daemon drivers"
	replaces="libvirt"
	mkdir -p "$subpkgdir"/usr/lib/libvirt \
		"$subpkgdir"/etc/libvirt/nwfilter
	mv "$pkgdir"/usr/lib/libvirt/connection-driver \
		"$subpkgdir"/usr/lib/libvirt/
	mv "$pkgdir"/etc/libvirt/nwfilter \
		"$subpkgdir"/etc/libvirt/
}

sha512sums="55091addcf43d3c0bdd50f9378b588351181d191272d5a19220a0babe0893c1f6e0f1e41a7f51b8c1fb8e2098236b273e1a18b81573f4008ee3cf65374ba9465  libvirt-6.6.0.tar.xz
c3647366e589f2134599baefcbd7ec480cbc0830c71f95cd903cacb28e463d4cd8d972fbd1db81aa17eef81c1f701bd3937c9b8eb71d56ee630e1ed8b89887e9  libvirt.confd
95debe7600826a6783334f5bf042fde81d0a01f2c599eb8d2f66e0597cf52116c13d4d5cf173784a360d134a7c44d9bf828e8f78f7e10a1ad9dada537aead480  libvirt.initd
78f419a89de7aabaad860903c24f1c070786fc1296d987e61dd537705b0e7f71a46c315888028f3826c3417e42861ca8471788be4ec5695e98427c5f18ae63fc  libvirt-guests.confd
ec2bc46d72afa61513983b777e2d49da618a35a4687bc75674b3a49ababd4e2e387661010d342fee86c7a73d259644985a263a6c2976e29a436d88921bb040b0  libvirt-guests.initd
f93313c387c21bafb760f599942f1cdecfe952af80af42a28e8525118e5a7fd512d9f909039d8b15f1a544957f1d70a0728c55de2bae41a87e5be6dbbb4f6208  virtlogd.initd
d8ef64d76e8be521a6551893cc7c6aa7e5d8c627a9aed87beefefc126798dc0f98bf420609b6afc027ad96cca347a1c5db8f703d09853efde4d1a6a35572fbf1  virtlockd.initd
dfe042c596028125bf8548115de2922683829c4716f6b0efb8efc38518670e3e848481661b9714bb0664c1022b87e8f3c0773611fe10187b0bc588e2336ada0c  musl-fix-includes.patch
d32551d56eae2c33bab7571964f76421336967918c293cd1d18fc66626a7ead3551b5672a9e3fc3d8b7bca82cc0abb17e428ee8271d04d73bc06fd16309918b3  stderr-fix.patch
4e7825f193d3931aa291fa05470e9081218e29231cfc91a5fe5982c04fb43e68ccc19cd40cb05f653da6cac5fb5d924ec2ef3fddbedda354ea40ff22a4d29697  libvirt-fork-exec-deadlock.patch
dc80c9fc459207dd1ff77548054ea5a51c41a2d36e1b8917eca5f69293a86168af3da47466d8ff954a8b2e0ead9f28ed7d6c2c3671cf67d0ebb4039003a0dde2  libvirt-6.0.0-fix_paths_in_libvirt-guests_sh.patch"
